package com.bigboy.bbs.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.bigboy.bbs.entity.Block;
import com.bigboy.bbs.entity.Type;
import com.bigboy.bbs.util.DBHelp;
import com.bigboy.bbs.util.Page;
import com.bigboy.bbs.util.RowMapper;

public class TypeDao {
	private DBHelp<Type> db = new DBHelp<Type>();
	public Type findById(String id) {
		String sql = "SELECT id,NAME,pid,flag,t_block_id FROM t_type WHERE id=? and flag= 1";
		return db.queryForObject(sql, new TypeMapper(), id);
	}
	public Type findByTypeId(String typeid, Page page) {
		String sql = "SELECT t.id,t.name,t.pid,t.flag,tie.id AS tieid,tie.title AS tietitle,tie.clickNum,tie.replyNum,tie.content,tie.createTime,tie.tid,tie.isTop,tie.isBest,tie.tag,tie.flag AS tieflag,tie.t_user_id,tie.t_type_id,u.id AS uid,u.name AS username,u.password,u.sex,u.email,u.tel,u.regTime,u.score,u.grade,u.pic,u.tieNum,u.sign,u.flag AS userflag,u.permission FROM t_type AS t LEFT JOIN t_tie AS tie ON t.id=tie.t_type_id LEFT JOIN t_user AS u ON tie.t_user_id=u.id WHERE t.id=? LIMIT ?,?";
		return db.queryForObject(sql, new TypeMapper(), typeid,page.getStartNum(),page.getPageSize());
	}
	public List<Type> findAll() {
		String sql = "SELECT id,NAME,pid,flag,t_block_id FROM t_type WHERE flag= 1";
		return db.queryForList(sql, new TypeMapper());
	}
}
class TypeMapper implements RowMapper<Type>{

	public Type mapRow(ResultSet rs) throws SQLException {
		Type type = new Type();
		type.setId(rs.getInt("id"));
		type.setFlag(rs.getInt("flag"));
		type.setName(rs.getString("name"));
		type.setPid(rs.getInt("pid"));
		
		Block block = new Block();
		block.setId(rs.getInt("t_block_id"));
		type.setBlock(block);
		return type;
	}

	
	
}